#include "ClientGame.h"

#ifdef _DEBUG
	#include <stdio.h>
	#include <fcntl.h>
	#include <io.h>
	#include <fstream>

	void EnableConsole();
#endif

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
#ifdef _DEBUG
	EnableConsole();
#endif

	ClientGame* game = ClientGame::GetInstance();

	game->Initialize();
	game->LoadContent();
	game->Run();
	game->UnloadContent();

	delete game;
}

#ifdef _DEBUG
void EnableConsole()
{
	int consoleHandle;
	long stdHandle;
	CONSOLE_SCREEN_BUFFER_INFO consoleInfo;
	FILE *file;
	
	const int bufferSize = 500;

	// allocate a console for this app
	AllocConsole();

	// set the screen buffer to be big enough to let us scroll text
	GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &consoleInfo);
	consoleInfo.dwSize.Y = bufferSize;
	SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), consoleInfo.dwSize);

	// redirect unbuffered STDOUT to the console
	stdHandle = (long)GetStdHandle(STD_OUTPUT_HANDLE);
	consoleHandle = _open_osfhandle(stdHandle, _O_TEXT);
	file = _fdopen(consoleHandle, "w");
	*stdout = *file;
	setvbuf(stdout, NULL, _IONBF, 0);

	// redirect unbuffered STDIN to the console
	stdHandle = (long)GetStdHandle(STD_INPUT_HANDLE);
	consoleHandle = _open_osfhandle(stdHandle, _O_TEXT);
	file = _fdopen(consoleHandle, "r");
	*stdin = *file;
	setvbuf(stdin, NULL, _IONBF, 0);

	// redirect unbuffered STDERR to the console
	stdHandle = (long)GetStdHandle(STD_ERROR_HANDLE);
	consoleHandle = _open_osfhandle(stdHandle, _O_TEXT);
	file = _fdopen(consoleHandle, "w");
	*stderr = *file;
	setvbuf(stderr, NULL, _IONBF, 0);

	// make cout, wcout, cin, wcin, wcerr, cerr, wclog and clog point to console as well
	std::ios::sync_with_stdio();
}
#endif